<?php
/* ========================================
 *  Utils.php
 * --------------------
 * Created on 24.03.2010
 * ========================================
 * $Revision: $
 * $Author: Dmitry Boycov
 * $HeadURL: $ 
 * $Id: $
 * ========================================
 * Changes ["date: description (who)"]:
 *
 * 24.03.2010: Original version (WHo)
 * 14:01:56: Time 
 */
require_once("app/App.php");

class Utils {

	public static $IMAGE_EXTENSIONS = array('gif','png','jpeg','jpg');
	public static $IMAGE_FILE = 'image';
	public static $DIRECTORY = 'directory';

	/**
	 * read directory
	 * @static
	 * @param  $dir - directory
	* @return array
	 */
	public static function readDir($dir,$type=null){
		$settings = App::settings();
		$files = array();
		try{
			$d=opendir($settings["APP_PATH"].'/'.$dir);
			while($f=readdir($d)){
				if($f!="."&&$f!=".."){
					if(!is_dir($dir."/".$f) && $type!=self::$DIRECTORY){
						$ext = strtolower(substr($f,strrpos($f, '.')+1));
						if($type == self::$IMAGE_FILE){
							if(in_array($ext,self::$IMAGE_EXTENSIONS)){
								array_push($files,$f);
							}
						}else{
							array_push($files,$f);
						}
					}else{
						if($type == self::$DIRECTORY){
							array_push($files,$f);
						}
					}
				}
			}
			closedir($d);
		}catch(Exception $e){
			header('HTTP/1.1 500 Internal Server Error');
			echo('<h1>Internal server error. Unable to read directory.</h1>');
		}
		return $files;
	}
}
?>